<div class="section" id="sitemaplist">

	<div class="section-title">Quản lý biên nhận</div>
    
    <div class="section-content padding1">
    
    	<form name="frm" id="frm" action="<?php echo $action?>" method="post" enctype="multipart/form-data">
        <input type="hidden" name="biennhanid" value="<?php echo $item['biennhanid']?>">	
            <div class="button right">
                <a class="button save" onclick="save('')">Lưu</a>
                <a class="button save" onclick="save('print')">Lưu & In</a>
                <a class="button cancel" href="?route=camdo/biennhan">Bỏ qua</a>    
        	</div>
            <div class="clearer">&nbsp;</div>
        	<div id="error" class="error" style="display:none"></div>
        	<div>   
                
                <p class="left">
                    <label>Ngày cầm</label><br />
                    <input type="text" name="ngaycam" value="<?php echo $this->date->formatMySQLDate($item['ngaycam'])?>" class="text ben-datepicker"/>
                    <input type="button" class="button" id="btnSelectKhachHang" value="Chọn khách hàng">
                </p>
                
                <div class="clearer">&nbsp;</div>
                <p class="left">
                	<label>Số CMND</label><br />
                    <input type="text" id="socmnd" name="socmnd" value="<?php echo $item['socmnd']?>" class="text gridautocomplete" size=60 />
                </p>
                <p class="left">
                	<label>Ngày cấp</label><br />
                    
                    <input type="text" id="ngaycap" name="ngaycap" value="<?php echo $this->date->formatMySQLDate($item['ngaycap'])?>" class="text ben-datepicker"/>
                </p>
                <div class="clearer">&nbsp;</div>
                <p class="left">
                    <label>Tên khách hàng</label><br />
                    <input type="hidden" id="khachhangid" name="khachhangid" value="<?php echo $item['khachhangid']?>">
                    <input type="text" id="tenkhachhang" name="tenkhachhang" value="<?php echo $item['tenkhachhang']?>" class="text gridautocomplete" size=60 />
                </p>
                
                <p class="left">
                    <label>Số điện thoại</label><br />
                    <input type="text" id="sodienthoai" name="sodienthoai" value="<?php echo $item['sodienthoai']?>" class="text" size=60 />
                </p>
                <p class="left">
                    <label>Email</label><br />
                    <input type="text" id="email" name="email" value="<?php echo $item['email']?>" class="text" size=60 />
                </p>
                
                <p class="left">
                    <label>Địa chỉ</label><br />
                    <input type="text" id="diachi" name="diachi" value="<?php echo $item['diachi']?>" class="text" size=60 />
                </p>
                <div class="clearer">&nbsp;</div>
                <p>
                	<label>Giá trị cầm</label></p>
                    <input type="text" id="giatricam" name="giatricam" value="<?php echo $item['giatricam']?>" class="text number"/>
                </p>
                <p>
                	<label>Nhóm tài sản</label><br />
                    <select id="nhomtaisanid" name="nhomtaisanid">
                    
                    </select>
                </p>
                <p>
                	<label>Lãi xuất</label><br>
                    <input type="text" id="laisuat" name="laisuat" value="<?php echo $item['laisuat']?>" class="text number"/>%
                </p>
                <p>
                	<label>Giảm lãi</label><br />
                    <input type="checkbox" id="giamlai" name="giamlai" value="1" <?php echo $item['giamlai']=='1'?'checked="checked"':'' ?>/>
                </p>
                <p>
                	<label>Mã số tài sản</label></p>
                    <input type="text" id="masotaisan" name="masotaisan" value="<?php echo $item['masotaisan']?>" class="text" size=60 />
                </p>
                <p>
                    <label>Mô tả</label><br />
                    <textarea name="motataisan" class="ghichu"><?php echo $item['motataisan']?></textarea>
                </p>
                
                <p>
                    <label>Ngày đến lãi</label><br />
                    <input type="text" name="ngaydenlai" value="<?php echo $this->date->formatMySQLDate($item['ngaydenlai'])?>" class="text ben-datepicker"/>
                    
                </p>
                <p>
                	<label>Số ngày quá hạn tối đa</label></p>
                    <input type="text" id="ngayquahantoida" name="ngayquahantoida" value="<?php echo $item['ngayquahantoida']?>" class="text number"/> ngày
                </p>
                <p>
                    <label>Ghi chú</label><br />
                    <textarea name="ghichu" class="ghichu"><?php echo $item['ghichu']?></textarea>
                </p>
                <p class="left">
                    <label>Lưu trữ</label><br />
                    <input type="text" id="luutru" name="luutru" value="<?php echo $item['luutru']?>" class="text" size=60 />
                </p>
                <div class="clearer">&nbsp;</div>
            </div>
            
        </form>
    
    </div>
    
</div>

<script language="javascript">

var auto = new AutoComplete();
function callBackAutoComplete(val)
{
	if(val.id == "socmnd")
		getKhachHangs("idnumber",val.value,"like");
	if(val.id == "tenkhachhang")
		getKhachHangs("fullname",val.value,"like");
	
}


function getKhachHangs(col,val,operator)
{
	$.getJSON("?route=core/member/getMembers&col="+col+"&val="+val+"&operator="+operator, 
			function(data) 
			{
				str = "";
				for( i in data.members)
				{
					str+= '<li id=rowauto-'+i+' class=autocompleterow>'+ data.members[i].fullname + ':' +data.members[i].idnumber+'</li><input type="hidden" id="idauto-'+i+'" value="'+ data.members[i].id +'" />';
				}
				$("#autocomplete").html("<ul class='autocomplete'>"+str+"</ul>");
				auto.selectRow();
			});
}

function selectValue(eid)
{
	
	var khachhangid = auto.getId();
	if(khachhangid)
	{
		$.getJSON("?route=core/user/getUser&id="+khachhangid,function(data){
			$('#khachhangid').val(data.id);
			$('#tenkhachhang').val(data.fullname);
			$('#socmnd').val(data.idnumber);
			$('#ngaycap').val(data.iddate);
			$('#sodienthoai').val(data.phone);
			$('#email').val(data.email);
			$('#diachi').val(data.address);
			
		});
	}
	auto.closeAutocomplete()
}

$('#btnSelectKhachHang').click(function(e) {
	$("#popup").attr('title','Chọn khách hàng');
				$( "#popup" ).dialog({
					autoOpen: false,
					show: "blind",
					hide: "explode",
					width: 800,
					height: 500,
					modal: true,
					buttons: {
						
						
						'Đóng': function() {
							$( this ).dialog( "close" );
						},
						'Chọn': function(){
							$("#listuser input[name*=\'delete\']'").each(function(index, element) {
								if(this.checked == true)
								{
                                	$.getJSON("?route=core/user/getUser&id="+this.value,function(data){
										$('#khachhangid').val(data.id);
										$('#socmnd').val(data.idnumber);
										$('#ngaycap').val(data.iddate);
										$('#tenkhachhang').val(data.fullname);
										$('#sodienthoai').val(data.phone);
										$('#email').val(data.email);
										$('#diachi').val(data.address);
										
									});
								}
                            });
							$( this ).dialog( "close" );
						},
						
					}
				});
			
				
				$("#popup-content").load("?route=core/member&dialog=true",function(){
					$("#popup").dialog("open");	
				});
    
});
$(document).ready(function(e) {
	auto.autocomplete();
	$('#nhomtaisanid').load("?route=camdo/nhomtaisan/getCbNhomTaiSan",function(){
		$('#nhomtaisanid').val("<?php echo $item['nhomtaisanid']?>");
		$(this).change();	
	});
});

$('#nhomtaisanid').change(function(e) {
    
	$.getJSON("?route=camdo/nhomtaisan/getNhomTaiSan&nhomtaisanid="+ $('#nhomtaisanid').val(),function(data){
		var giatricam = $('#giatricam').val().replace(/,/g,"");
		var laisuatapdung = 0;
		for(i in data.laisuat)
		{
			if(Number(giatricam) >= Number(data.laisuat[i].sotien))
			{
				laisuatapdung =data.laisuat[i].laisuat;	
			}
			
		}
		$('#laisuat').val(laisuatapdung);
	});
});
$('#giatricam').change(function(e) {
    $('#nhomtaisanid').change();
});
function save(action)
{
	$.blockUI({ message: "<h1>Please wait...</h1>" }); 
	
	$.post("?route=camdo/biennhan/save", $("#frm").serialize(),
		function(data){
			var arr = data.split("-");
			if(arr[0] == "true")
			{
				if(action == 'print')
				{
					view(arr[1])
				}
				else
				{
					window.location = "?route=camdo/biennhan";	
				}
			}
			else
			{
			
				$('#error').html(data).show('slow');
				$.unblockUI();
				
			}
			
		}
	);
}
function view(biennhanid)
{
	$("#popup").attr('title','Chọn khách hàng');
				$( "#popup" ).dialog({
					autoOpen: false,
					show: "blind",
					hide: "explode",
					width: 800,
					height: 500,
					modal: true,
					buttons: {
						'Đóng': function() {
							$( this ).dialog( "close" );
							window.location = "?route=camdo/biennhan";
						},
						'In': function(){
							openDialog("?route=camdo/biennhan/view&biennhanid="+biennhanid+"&dialog=print",800,500)
							window.location = "?route=camdo/biennhan";
						},
					}
				});
			
				
	$("#popup-content").load("?route=camdo/biennhan/view&biennhanid="+biennhanid+"&dialog=true",function(){
		$("#popup").dialog("open");	
	});
}
</script>
